﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Eflong.MonitoringLib.MonitoringUnit;
using Eflong.Structure;
using Eflong.Utility;

namespace KWBridge
{
    internal class ShowSensorInfos
    {
    }

    /// <summary>
    /// 报警类型选项
    /// </summary>
    public class AlarmOptions
    {
        public bool s超上限 = false;
        public bool s超下限 = false;

        public MonitoringUnitState.States[ ] states;
        public List<MonitoringUnitState.States> list;

        public AlarmOptions( )
        {
            list = new List<MonitoringUnitState.States>( );
        }

        public void ClearAll( )
        {
            list.Clear( );
        }

        public void AddMonitoringUnitState( MonitoringUnitState.States state )
        {
            list.Add( state );
        }

        public void AddStateName( string stateName )
        {
            MonitoringUnitState.States state = MonitoringUnitState.GetEflongStates( stateName );
            list.Add( state );
        }

        public MonitoringUnitState.States[ ] GetAlarmOptions( )
        {
            //if (s超上限)
            //{
            //    list.Add("超上限");
            //}
            //else if (s超下限)
            //{
            //    list.Add("超下限");
            //}
            //{
            //}
            return list.ToArray( );
        }
    }

    /// <summary>
    /// 传感器类型选项
    /// </summary>
    public class TypeOptions
    {
        public bool s甲烷 = true;

        public List<MonitoringUnitType.Types> list_Types = new List<MonitoringUnitType.Types>( );

        public TypeOptions( )
        {
        }

        public void ClearAll( )
        {
            list_Types.Clear( );
        }

        public void AddType( MonitoringUnitType.Types type )
        {
            list_Types.Add( type );
        }

        public MonitoringUnitType.Types[ ] GetTypeOptions( )
        {
            return list_Types.ToArray( );
        }

        public void AddTypeName( string sType )
        {
            switch ( sType )
            {
                case "甲烷":
                    this.AddType( MonitoringUnitType.Types.甲烷 );
                    break;

                case "一氧化碳":
                    this.AddType( MonitoringUnitType.Types.一氧化碳 );
                    break;

                case "风速":
                    this.AddType( MonitoringUnitType.Types.风速 );
                    break;

                case "温度":
                    this.AddType( MonitoringUnitType.Types.温度 );
                    break;

                case "压力":
                    this.AddType( MonitoringUnitType.Types.压力 );
                    break;

                case "全部模":
                    this.AddType( MonitoringUnitType.Types.全部模 );
                    break;

                case "主扇":
                    this.AddType( MonitoringUnitType.Types.主扇 );
                    break;

                case "局扇":
                    this.AddType( MonitoringUnitType.Types.局扇 );
                    this.AddType( MonitoringUnitType.Types.风机 );
                    break;

                case "烟雾":
                    this.AddType( MonitoringUnitType.Types.烟雾 );
                    break;

                case "断电":
                case "断电器":
                    this.AddType( MonitoringUnitType.Types.断电 );
                    break;

                case "风门":
                    this.AddType( MonitoringUnitType.Types.风门 );
                    break;

                case "全部开":
                    this.AddType( MonitoringUnitType.Types.全部开 );
                    break;
            }
        }
    }

    public enum ItemStates { None = 0 , 新增 = 1 , 修改 = 2 , 删除 = 3 , Update = 4 }

    public enum ItemIndexs
    {
        sensorName = 1 , sensorType = 2 , sValue = 3 ,
        sTime = 4 , sUnit = 2 , hightValue = 5 , lowValue = 6 ,
        hightLimit = 7 , lowLimit = 8 , breakValue = 9 , restValue = 10
    };

    #region 联网传感器的显示类

    public delegate void WindowAlarm( ItemInfo info );

    public delegate void WindowDelete( ItemInfo info );

    public class ItemInfo : IDisposable
    {
        public string sensorId = string.Empty;
        public string sensorName = string.Empty;
        public string sensorType = string.Empty;
        public string sValue = string.Empty;
        public string sTime = string.Empty;
        public string sUnit = string.Empty;
        public string lowValue = string.Empty;
        public string hightValue = string.Empty;
        public string lowLimit = string.Empty;
        public string hightLimit = string.Empty;
        public string breakValue = string.Empty;
        public string restValue = string.Empty;

        public string sAlarmString = string.Empty;

        public bool IsAlert;

        //public BaseMonitoringUnit unit;
        /*
         *  语音报警类
         */

        /// <summary>
        /// 是否语音报警
        /// </summary>
        public bool IsAlarm;

        /// <summary>
        /// 语音报警开始时刻

        /// </summary>
        public DateTime tmAlarmStart;

        public bool IsFault;

        /// <summary>
        /// 当前状态
        /// </summary>
        public MonitoringUnitState.States state;

        public ItemStates sState;

        public static WindowAlarm windowAlarm;

        public static WindowDelete windowDelete;

        public ItemInfo( string sensorId )
        {
            this.sensorId = sensorId;
            sState = ItemStates.None;
        }

        ~ItemInfo( )
        {
            Dispose( false );
        }

        public void Dispose( )
        {
            Dispose( true );

            System.GC.SuppressFinalize( this );

            // 上面一行代码作用是防止"垃圾回收器"调用这个类中的方法
            // " ~ResourceHolder() "
            // 为什么要防止呢? 因为如果用户记得调用Dispose()方法,那么
            // "垃圾回收器"就没有必要"多此一举"地再去释放一遍"非托管资源"了
            // 如果用户不记得调用呢,就让"垃圾回收器"帮我们去"多此一举"吧 ^_^
            // 你看不懂我上面说的不要紧,下面我还有更详细的解释呢!
        }

        protected virtual void Dispose( bool disposing )
        {
            if ( disposing )
            {
                // 这里是清理"托管资源"的用户代码段
            }

            // 这里是清理"非托管资源"的用户代码段
        }

        public ItemInfo( string sensorId , string sensorName , string sensorType , string sValue , string sTime )
        {
            this.sensorId = sensorId;
            this.sensorName = sensorName;
            this.sensorType = sensorType;
            this.sValue = sValue;
            this.sTime = sTime;
        }

        public void SetAnalogInfo( string sUnit , string hightValue , string lowValue , string hightLimit , string lowLimit , string breakValue , string restValue )
        {
            this.sUnit = sUnit;
            this.hightValue = hightValue;
            this.lowValue = lowValue;
            this.hightLimit = hightLimit;
            this.lowLimit = lowLimit;
            this.breakValue = breakValue;
            this.restValue = restValue;
        }

        public void ConvertFromMonitoringUnit( BaseMonitoringUnit unit )
        {
            ItemInfo info = this;

            //info.unit = unit;
            info.sensorId = unit.m_strId;
            info.sensorName = unit.m_strName;
            info.sensorType = unit.m_Type.ToString( );
            info.SetItemState( unit.modifyFlag.ToString( ) );
            info.state = unit.m_State;
            try
            {
                if ( MonitoringUnitState.IsFault( unit.m_State ) )
                {
                    info.sTime = Common.GetDateTimeString( unit.m_timeStateChange );
                    MonitoringUnitState.ChineseStates s = ( MonitoringUnitState.ChineseStates )unit.m_State;
                    info.sValue = s.ToString( );
                    info.IsFault = true;
                }
                else
                {
                    info.IsFault = false;
                    if ( unit.m_Modality == MonitoringUnitModality.Modalities.Analog )
                    {
                        info.sTime = Common.GetDateTimeString( unit.m_timeData );
                        info.sValue = Common.GetStringFrom( unit.m_fValue ) + unit.m_strUnit;
                        if ( unit.m_State != MonitoringUnitState.States.Normal )
                        {
                            info.IsAlert = true;
                        }
                        else
                        {
                            info.IsAlert = false;
                        }
                    }
                    else
                    {
                        info.sTime = Common.GetDateTimeString( unit.m_timeStateChange );
                        info.sValue = unit.m_strStateName[ ( int )unit.m_fValue ];
                        if ( unit.m_State == MonitoringUnitState.States.State0 )
                        {
                            info.IsAlert = false;
                        }
                        else
                        {
                            info.IsAlert = true;
                        }
                    }
                }

                if ( unit.m_Modality == MonitoringUnitModality.Modalities.Analog )
                {
                    info.sUnit = unit.m_strUnit;
                    info.hightValue = Common.GetStringFrom( unit.m_fUpperBound );
                    info.lowValue = Common.GetStringFrom( unit.m_fLowerBound );
                    info.hightLimit = Common.GetStringFrom( unit.m_fUpperLimit );
                    info.lowLimit = Common.GetStringFrom( unit.m_fLowerLimit );
                    info.breakValue = Common.GetStringFrom( unit.m_fBreakLimit );
                    info.restValue = Common.GetStringFrom( unit.m_fRestLimit );
                }
                else
                {
                    info.lowValue = unit.m_strStateName[ 0 ];
                    info.hightValue = unit.m_strStateName[ 1 ];
                    info.hightLimit = unit.m_strStateName[ 2 ];
                }
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "ShowSensorInfo->info 测点号：" + info.sensorId + " 测点名：" + info.sensorName + " , " + ex.Message );
            }
        }

        public static string GetAlarmString( BaseMonitoringUnit unit )
        {
            StringBuilder sb = new StringBuilder( );
            sb.Append( Common.GetChineseNumberString( unit.m_strName ) );
            sb.Append( " " );
            sb.Append( unit.m_Type.ToString( ) );
            sb.Append( " " );
            if ( unit.m_State > MonitoringUnitState.States.OverUpperLimit )
            {
                //info.sTime = Common.GetDateTimeString(unit.m_timeStateChange);
                MonitoringUnitState.ChineseStates s = ( MonitoringUnitState.ChineseStates )unit.m_State;
                sb.Append( s.ToString( ) );
            }
            else
            {
                if ( unit.m_Modality == MonitoringUnitModality.Modalities.Digital )
                {
                    //info.sTime = KWConvert.GetDateTimeString(unit.m_timeStateChange);
                    //info.sValue = unit.m_strStateName[(int)unit.m_fValue];
                    sb.Append( unit.m_strStateName[ ( int )unit.m_fValue ] );
                }
                else
                {
                    MonitoringUnitState.ChineseStates s = ( MonitoringUnitState.ChineseStates )unit.m_State;
                    sb.Append( s.ToString( ) );
                    sb.Append( " " );
                    sb.Append( "当前值" );
                    sb.Append( " " );
                    sb.Append( Common.GetChineseStringFrom( unit.m_fValue , unit.m_strUnit ) );
                }
            }
            return sb.ToString( );
        }

        public bool CheckAlarm( AlarmOptions alarmOptions , TypeOptions typeOptions , BaseMonitoringUnit unit , Speech speech )
        {
            if ( typeOptions.list_Types.Contains( unit.m_Type ) )
            {
                bool bAlarm = false;

                if ( this.state == MonitoringUnitState.States.State1 || this.state == MonitoringUnitState.States.State2 || alarmOptions.list.Contains( this.state ) )
                {
                    // 判断是否是开关量的状态1 或状态2，均认为是异常状态

                    if ( !this.IsAlarm )
                    {
                        IsAlarm = true;
                        tmAlarmStart = DateTime.Now;
                    }
                    if ( unit.m_Type != MonitoringUnitType.Types.局扇 && unit.m_Type != MonitoringUnitType.Types.主扇 && unit.m_Type != MonitoringUnitType.Types.风机 )
                    {
                        this.sAlarmString = GetAlarmString( unit );
                        SpeechAlarm( speech , this );
                    }
                    if ( windowAlarm != null )
                    {
                        windowAlarm( this );
                    }
                }
                else
                {
                    if ( this.IsAlarm )
                    {
                        IsAlarm = false;

                        //tmAlarmStart = DateTime.Now;
                        this.sAlarmString = string.Empty;
                        SpeechDelete( speech , this );

                        if ( windowDelete != null )
                        {
                            windowDelete( this );
                        }
                    }
                }

                return bAlarm;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 进行语音报警
        /// </summary>
        /// <param name="speech"></param>
        public static void SpeechAlarm( Speech speech , ItemInfo info )
        {
            if ( speech != null )
            {
                speech.AddSpeech( info.sensorId , info.sensorType , info.sAlarmString );
            }
        }

        /// <summary>
        /// 删除语音报警
        /// </summary>
        /// <param name="speech"></param>
        public static void SpeechDelete( Speech speech , ItemInfo info )
        {
            if ( speech != null )
            {
                speech.DeleteSpeech( info.sensorId );
            }
        }

        ///// <summary>
        ///// 加入一个报警信息
        ///// </summary>
        ///// <param name="alertForm"></param>
        ///// <param name="info"></param>
        //public static void WindowAlarm(AlertForm alertForm, ItemInfo info)
        //{
        //    if (alertForm != null)
        //    {
        //        try
        //        {
        //            alertForm.AddItemInfo(info, true);
        //        }
        //        catch (System.Exception ex)
        //        {
        //            Console.WriteLine(ex.Message);
        //            try
        //            {
        //                alertForm.ExitAlertForm();
        //            }
        //            finally
        //            {
        //                alertForm = null;
        //            }
        //        }

        //    }
        //}
        ///// <summary>
        ///// 解除一个报警信息
        ///// </summary>
        ///// <param name="alertForm"></param>
        ///// <param name="info"></param>
        //public static void WindowDelete(AlertForm alertForm, ItemInfo info)
        //{
        //    if (alertForm != null)
        //    {
        //        try
        //        {
        //            alertForm.AddItemInfo(info, false);
        //        }
        //        catch (System.Exception ex)
        //        {
        //            Console.WriteLine(ex.Message);
        //            try
        //            {
        //                alertForm.ExitAlertForm();
        //            }
        //            finally
        //            {
        //                alertForm = null;
        //            }
        //        }
        //    }
        //}

        public void SetItemState( string state )
        {
            switch ( state )
            {
                case "None":
                    this.sState = ItemStates.None;
                    break;

                case "新增":
                    this.sState = ItemStates.新增;
                    break;

                case "修改":
                    this.sState = ItemStates.修改;
                    break;

                case "删除":
                    this.sState = ItemStates.删除;
                    break;
                default:
                    this.sState = ItemStates.None;
                    break;
            }
        }

        public static ListViewItem GetListViewItem( ItemInfo info )
        {
            ListViewItem item = new ListViewItem( );
            item.Text = info.sensorId;
            item.SubItems.Add( info.sensorName );
            item.SubItems.Add( info.sensorType );
            item.SubItems.Add( info.sValue );
            item.SubItems.Add( info.sTime );
            item.SubItems.Add( info.hightValue );
            item.SubItems.Add( info.lowValue );
            item.SubItems.Add( info.hightLimit );
            item.SubItems.Add( info.lowLimit );
            item.SubItems.Add( info.breakValue );
            item.SubItems.Add( info.restValue );
            return item;
        }
    }

    #endregion 联网传感器的显示类
}